package secondriver.sudo.activity;

import static secondriver.sudo.activity.AppConstant.GAME_LEVEL_KEY;
import static secondriver.sudo.activity.AppConstant.GAME_SHARE_PREFERENCES;
import static secondriver.sudo.activity.AppConstant.GAME_PROGRESS_STORE_KEY;
import static secondriver.sudo.activity.AppConstant.GAME_PROGRESS_STORE_PREFIX;

import secondriver.sudo.R;
import secondriver.sudo.bus.GameLevel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private static final String TAG = "MainActivity";

	private Button baseBtn;
	private Button primaryBtn;
	private Button intermediateBtn;
	private Button advancedBtn;

	private SharedPreferences preferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initComponent();
		preferences = getSharedPreferences(GAME_SHARE_PREFERENCES, MODE_PRIVATE);
	}

	private void initComponent() {
		Log.i(TAG, "initComponent");
		baseBtn = (Button) findViewById(R.id.base_btn);
		primaryBtn = (Button) findViewById(R.id.primary_btn);
		intermediateBtn = (Button) findViewById(R.id.intermediate_btn);
		advancedBtn = (Button) findViewById(R.id.advanced_btn);

		baseBtn.setOnClickListener(this);
		primaryBtn.setOnClickListener(this);
		intermediateBtn.setOnClickListener(this);
		advancedBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		this.initComponent();
		final Intent intent = new Intent();
		int level = GameLevel.BASE.getV();
		switch (v.getId()) {
		case R.id.base_btn:
			level = GameLevel.BASE.getV();
			break;
		case R.id.primary_btn:
			level = GameLevel.PRIMARY.getV();
			break;
		case R.id.intermediate_btn:
			level = GameLevel.INTERMEDIATE.getV();
			break;
		case R.id.advanced_btn:
			level = GameLevel.ADVANCED.getV();
			break;
		default:
			level = GameLevel.BASE.getV();
			break;
		}
		v.setBackgroundColor(android.R.color.holo_orange_dark);

        intent.setClass(this, GameActivity.class);
        intent.putExtra(GAME_LEVEL_KEY, level);

        final String storeValue = preferences.getString(
                GAME_PROGRESS_STORE_PREFIX + level, "");
        if (!TextUtils.isEmpty(storeValue)) {
			new AlertDialog.Builder(this)
					.setIcon(android.R.drawable.ic_dialog_info)
					.setTitle(R.string.game_dialog_title)
					.setMessage(R.string.game_dialog_msg)
					.setPositiveButton(android.R.string.ok,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									intent.putExtra(GAME_PROGRESS_STORE_KEY,
											storeValue);
									startActivity(intent);
								}
							})
					.setNegativeButton(android.R.string.cancel,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									startActivity(intent);
								}

							}).show();
		} else {
			startActivity(intent);
		}
	}
}